﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows;
using System.Net;
using System.IO;
using System.Web;

namespace RI
{
    public partial class fromMain : Form
    {
        #region 程序初始化
        /// <summary>
        /// 注册信息结构体
        /// </summary>
        public struct RegInfo
        {
            public string username;
            public string password;
            public string truename;
            public string email;
            public string mobile;
            public string company;
            public string telephone;
            public string passport;
            public string sid;
        }
        /// <summary>
        /// 友情链接数据结构
        /// </summary>
        public struct Randomlink
        {
            public string title;
            public string url;
        }
        /// <summary>
        /// 声明注册信息结构体
        /// </summary>
        RegInfo regInfo = new RegInfo();
        /// <summary>
        /// 声明链接信息结构体
        /// </summary>
        Randomlink[] links = new Randomlink[20];
        /// <summary>
        /// 友情链接当前指针
        /// </summary>
        int link_num = 0;
        /// <summary>
        /// 当前新闻指针编号
        /// </summary>
        int newsCurrent = 0;
        /// <summary>
        /// 产品指针编号
        /// </summary>
        int productsCurrent = 0;
        /// <summary>
        /// 初始化小视窗
        /// </summary>
        CurrentUser currentUser;
        /// <summary>
        /// 鼠标坐标全局事件
        /// </summary>
        private MouseHook _mouse = new MouseHook();
        /// <summary>
        /// 构造函数
        /// </summary>
        public fromMain()
        {
            InitializeComponent();            
            currentUser = new CurrentUser();    //实例化小视窗

            //using (SystemConfig sc = new SystemConfig())
            //{
            //    this.Cursor = sc.SetCurrentCursor("Skins/Arrow.ani", this.Cursor);
            //    txtLocalhost.Cursor = sc.SetCurrentCursor("Skins/IBeam.ani", txtLocalhost.Cursor);
            //}

            //this._mouse.Start();    //监听坐标
            //this._mouse.OnMouseActivity += new MouseEventHandler(OnMouseActivity);  //监听事件
            VisualPoint();  //定位小视窗
        }
        #endregion

        /// <summary>
        /// 添加选项卡
        /// </summary>
        /// <param name="url">Url地址</param>
        /// <param name="title">网站标题:格式N|TITLE (N = 数字)</param>
        public void AddTablePage(string url, string title)
        {
            try
            {
                if (title.Length > 9) title = title.Substring(0, 9);
                Uri uri = new Uri(url);
                WebBrowser web = new WebBrowser();
                web.Url = uri;
                web.Dock = DockStyle.Fill;
                web.ScriptErrorsSuppressed = true; //不显示网页出错提示
                int count = tctPageMain.TabPages.Count;
                tctPageMain.TabPages.Add(title);
                tctPageMain.TabPages[count].Controls.Add(web);
                tctPageMain.SelectedIndex = count;
                tolState.Text = "打开网站: " + url;
                ShowVisual();
            }
            catch (Exception ex)
            {
                tolState.Text = "注意:" + ex.Message;
            }
            GC.Collect();
        }
        /// <summary>
        /// 绑定网址列表
        /// </summary>
        public void BindSiteList()
        {
            using (Business bus = new Business())
            {
                dgvSource.DataSource = bus.GetSiteNot();
            }
        }
        /// <summary>
        /// 显示小视窗
        /// </summary>
        public void ShowVisual()
        {
            if (tctPageMain.TabCount > 0)
            {
                int current = tctPageMain.SelectedIndex;
                string[] sid = tctPageMain.TabPages[current].Text.Split('|');
                if (sid.Length >= 1)
                {
                    currentUser.BindIDSite(sid[0].ToString());
                }
            }
            else
            {
                tolState.Text = "请先打开一个网站...";
            }
        }
        /// <summary>
        /// 定位小视窗
        /// </summary>
        private void VisualPoint()
        {
            int xWidth = SystemInformation.PrimaryMonitorMaximizedWindowSize.Width;
            int yHeight = SystemInformation.PrimaryMonitorMaximizedWindowSize.Height;
            xWidth -= (currentUser.Width + 30);
            yHeight -= (currentUser.Height + 53);
            currentUser.Location = new Point(xWidth, yHeight);
        }

        #region 按钮事件
        //程序加载时,初始化各控件功能
        private void fromMain_Load(object sender, EventArgs e)
        {
            this.BindSiteList();
            tctPageMain.TabPages.Clear();
            btnSave.Enabled = false;
            btnUserToSiteOK.Enabled = false;
            tolState.Text = "提示:程序加载完毕！操作过程中，“双击”鼠标左键可关闭网页选项卡。";

            //广告
            AddTablePage("http://www.taobaolm.org", "0|淘宝导购网");

            NotifyIcon notIfIcon = new NotifyIcon();
            notIfIcon.Icon = new Icon("enum.ico");
            notIfIcon.Text = "亲亲宝贝";
            notIfIcon.Visible = true;
            notIfIcon.ContextMenuStrip = contMenuNotifyIcon;
            notIfIcon.MouseDoubleClick += new MouseEventHandler(NotIfMouseDouble);
            string msg = "这是一款可以让你轻松愉快完成工作的好助手！\n还等什么？赶快发现更多奥秘吧!";
            notIfIcon.ShowBalloonTip(5000, "欢迎使用", msg,ToolTipIcon.Info);
        }
        //选择用户，登录当前网站
        private void btnSetLogin_Click(object sender, EventArgs e)
        {
            if (tctPageMain.TabCount != 0)
            {
                int current = tctPageMain.SelectedIndex;
                string[] sid = tctPageMain.TabPages[current].Text.Split('|');
                if (sid.Length != 0)
                {
                    //2.获取用户名和密码
                    Business.UserList user = new Business.UserList();
                    Frm_ShowUser showUser = new Frm_ShowUser(sid[0].ToString());
                    showUser.ShowDialog();
                    user = showUser.GetUser();

                    //3.设置登录口令和密码
                    if (user.username != "A" && user.password != "A")
                    {
                        //4.获取当前Document对象
                        HtmlDocument doc = null;
                        foreach (WebBrowser web in tctPageMain.TabPages[current].Controls)
                        {
                            doc = web.Document;
                        }
                        this.SetLoginUser(doc, user.username, user.password);
                    }
                    else
                    {
                        tolState.Text = "没有选择用户账户.";
                    }
                }
            }
            else
            {
                tolState.Text = "请先打开一个网站！";
            }
        }
        //双击选项卡，执行关闭
        private void tctPageMain_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (tctPageMain.TabPages.Count > 1)
            {
                int current = tctPageMain.SelectedIndex;
                foreach (WebBrowser web in tctPageMain.TabPages[current].Controls)
                {
                    web.Dispose();
                }
                tctPageMain.TabPages[current].Dispose();

                if(current == 0) tctPageMain.SelectedIndex = 0;
                else tctPageMain.SelectedIndex = current - 1;
            }
            GC.Collect();
        }
        //初始化注册页面
        private void btnSetRegister_Click(object sender, EventArgs e)
        {
            if (tctPageMain.TabCount != 0)
            {
                int current = tctPageMain.SelectedIndex;
                HtmlDocument doc = null;
                foreach (WebBrowser web in tctPageMain.TabPages[current].Controls)
                {
                    doc = web.Document;
                }
                //2. 初始化页面值
                RandomRegInfo();
                string[] sid = tctPageMain.TabPages[current].Text.Split('|');
                regInfo.sid = sid[0].ToString();

                //3. 设置页面值
                if (doc != null)
                    SetPageInfo(doc);
                else
                    tolState.Text = "请等待页面加载完毕...";
            }
            else
            {
                tolState.Text = "请先打开一个网站!";
            }
        }
        //保存注册用户信息
        private void btnSave_Click(object sender, EventArgs e)
        {
            string msg = string.Format("确定保存如下注册用户信息吗？\n\n分  类：{0}\n用户名：{1}\n密  码：{2}",
                regInfo.sid,regInfo.username, regInfo.password);
            if (MessageBox.Show(msg, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                using (Business bus = new Business())
                {
                    bus.AddUser(regInfo.sid, regInfo.username, regInfo.password, "暂无", "新注册用户"); //添加注册用户
                    bus.SetIsRegLable(regInfo.sid); //设置网址注册标志
                    this.BindSiteList();
                }
                tolState.Text = "用户注册信息保存成功。";
                btnSave.Enabled = false;
            }
            else
            {
                tolState.Text = "用户撤销了保存操作。";
            }
        }
        //网页选项卡之间切换时
        private void tctPageMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int current = tctPageMain.SelectedIndex;
                foreach (WebBrowser web in tctPageMain.TabPages[current].Controls)
                {
                    if (web != null)
                        txtLocalhost.Text = web.Document.Url.AbsoluteUri;
                }
                ShowVisual();
            }
            catch { }            
            GC.Collect();
        }
        //单击网页选项卡时
        private void tctPageMain_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                int current = tctPageMain.SelectedIndex;
                foreach (WebBrowser web in tctPageMain.TabPages[current].Controls)
                {
                    if (web != null)
                    {
                        txtLocalhost.Text = web.Document.Url.AbsoluteUri;   //显示当前网址
                        txtP_Catid.Text = LoadCatid(web.Document.Url.Host); //加载分类ＩＤ编号
                    }
                }
                ShowVisual();   //显示小视窗                
                
            }
            catch { }
            GC.Collect();
        }
        //双击网址时
        private void dgvSource_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (dgvSource.SelectedRows.Count != 0)
            {
                string url = dgvSource.SelectedRows[0].Cells[1].Value.ToString();
                string title = dgvSource.SelectedRows[0].Cells[0].Value.ToString() + "|" +
                    dgvSource.SelectedRows[0].Cells[2].Value.ToString();
                AddTablePage(url, title);

                groupBox3.Enabled = true;
                groupBox4.Enabled = true;
                groupBox5.Enabled = true;
                groupBox6.Enabled = true;
            }
            else
            {
                MessageBox.Show("请先添加网址!", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //手工导航网站时
        private void btnGoLocalhost_Click(object sender, EventArgs e)
        {
            if (txtLocalhost.Text.Trim() == string.Empty)
            {
                tolState.Text = "注意:网址不能为空!";
            }
            else
            {
                AddTablePage(txtLocalhost.Text.Trim(), "0|网站");
            }
        }
        //刷新
        private void btnRelease_Click(object sender, EventArgs e)
        {
            this.BindSiteList();
        }
        //初始化公司简介
        private void btnSetCompany_Click(object sender, EventArgs e)
        {
            try
            {
                if (tctPageMain.TabCount != 0)  //判断选项卡总数是否等于0
                {
                    int current = tctPageMain.SelectedIndex;
                    HtmlDocument doc = null;
                    foreach (WebBrowser web in tctPageMain.TabPages[current].Controls)
                    {
                        doc = web.Document;
                    }
                    SetCompanyInfo(doc);    //初始化公司简介
                }
                else tolState.Text = "请先打开一个网站！";
            }
            catch
            {
                tolState.Text = "这样还会出错，人品问题吧！";
            }
        }
        //导出数据时
        private void btnUserGoSite_Click(object sender, EventArgs e)
        {
            //1.禁用相关控件
            groupBox3.Enabled = false;
            groupBox4.Enabled = false;
            groupBox5.Enabled = false;
            
            btnUserToSiteOK.Enabled = true;
            btnUserGoSite.Enabled = false;

            //2.绑定控件数据
            using (Business bus = new Business())
            {
                dgvSource.DataSource = bus.GetAllRegisterUser();
            }

            //3.初始化控件
            if (dgvSource.Rows.Count != 0)
            {
                string url = "http://cnc6180.chinaw3.com/Add-ZhuChe.aspx";
                string title = "0|导出账号";
                AddTablePage(url, title);

                dgvSource.MouseDoubleClick -= new MouseEventHandler(dgvSource_MouseDoubleClick);
                dgvSource.MouseDoubleClick += new MouseEventHandler(dgv_MouseClick);
            }
        }
        //导出完成时
        private void btnUserToSiteOK_Click(object sender, EventArgs e)
        {
            dgvSource.MouseDoubleClick -= new MouseEventHandler(dgv_MouseClick);
            dgvSource.MouseDoubleClick += new MouseEventHandler(dgvSource_MouseDoubleClick);

            this.BindSiteList();
            if (dgvSource.Rows.Count != 0)
            {
                groupBox3.Enabled = true;
                groupBox4.Enabled = true;
                groupBox5.Enabled = true;
            }
            btnUserToSiteOK.Enabled = false;
            btnUserGoSite.Enabled = true;
        }
        //显示已注册用户
        private void btnShowUser_Click(object sender, EventArgs e)
        {
            frm_User user = new frm_User();
            user.Show();
        }
        //POST批量添加链接
        private void btnPostLink_Click(object sender, EventArgs e)
        {
            if (tctPageMain.TabCount != 0)   //判断选项卡总数是否等于0
            {
                int current = tctPageMain.SelectedIndex;
                foreach (WebBrowser web in tctPageMain.TabPages[current].Controls)
                {
                    PostLink(web);
                }
                GC.Collect();
            }
            else
            {
                tolState.Text = "请先打开一个网站!";
            }
        }
        //POST批量添加(HTML)新闻
        private void btnPostNews_Click(object sender, EventArgs e)
        {
            if (tctPageMain.TabCount != 0) { //判断选项卡总数是否等于0
                int current = tctPageMain.SelectedIndex;
                HtmlDocument doc = null;
                foreach (WebBrowser web in tctPageMain.TabPages[current].Controls){
                    doc = web.Document;
                }

                if (doc != null)    //document获取成功情况下
                {
                    Business bus = new Business();
                    DataTable dt = new DataTable();
                    dt = bus.GetItemCatNews("行业新闻");
                    if (dt.Rows.Count != 0)
                    {
                        if (newsCurrent >= dt.Rows.Count) newsCurrent = 0;
                        #region 检索页面元素
                        for (int i = 0; i < doc.All.Count; i++)
                        {
                            if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
                            {
                                try
                                {
                                    switch (doc.All[i].Name) 
                                    {
                                        case "post[title]": 
                                            //编号,标题,内容,UBB代码,分类
                                            if (dt.Rows[newsCurrent][1].ToString().Length > 50)
                                            {
                                                doc.All[i].InnerText = dt.Rows[newsCurrent][1].ToString().Substring(0, 50);
                                            } else {
                                                doc.All[i].InnerText = dt.Rows[newsCurrent][1].ToString();
                                            }
                                            Clipboard.Clear();
                                            Clipboard.SetText(dt.Rows[newsCurrent][2].ToString());  //内容复制到剪贴板
                                            break;
                                    }
                                }
                                catch { }
                            }
                        }
                        #endregion
                        newsCurrent++;
                    } else tolState.Text = "没有新闻数据可供添加。";
                } else tolState.Text = "当前文档元素获取失败。";
                GC.Collect();
            } else tolState.Text = "请先打开一个网站!";
        }
        //POST批量添加(UBB)新闻
        private void btnU_PNews_Click(object sender, EventArgs e)
        {
            PostMethNews(3);
        }
        //添加(HTML)版内容产品
        private void btnSetProducts_Click(object sender, EventArgs e)
        {
            AutoAddProducts(2);
            GC.Collect();
        }
        //添加(UBB)版内容产品
        private void btnU_Proc_Click(object sender, EventArgs e)
        {
            AutoAddProducts(3);
            GC.Collect();
        }
        //在托盘中选择退出主程序
        private void tolScript_ExitProgram_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定关闭程序吗?", "提示",
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Question) == DialogResult.OK)
            {
                this.Dispose();
                currentUser.Dispose();
                Application.Exit();
            }
        }
        //在托盘中选择显示程序时
        private void tolScript_DisplayMainProgram_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            this.Show();
            currentUser.Show();
        }
        //将窗体最小化至右下角
        private void fromMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
            currentUser.Hide();
        }
        //双击托盘图标时
        private void NotIfMouseDouble(object send, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            this.Show();
            currentUser.Show();
        }
        //显示小视窗
        private void btnShowVisual_Click(object sender, EventArgs e)
        {
            VisualPoint();
            ShowVisual();
            currentUser.Show();
        }
        //主程序大小改变时
        private void fromMain_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                currentUser.Hide();
            }
            else
            {
                currentUser.Show();
            }
        }
        //当前鼠标坐标监听事件
        private void OnMouseActivity(object send, MouseEventArgs e)
        {
            tolMousePixel.Text = string.Format("X:{0}  Y:{1}", e.X.ToString(), e.Y.ToString());
        }
        //保存分类ID
        private void btnSaveItemCid_Click(object sender, EventArgs e)
        {
            if (txtP_Catid.Text.Trim() != string.Empty &&
                txtLocalhost.Text.Trim() != string.Empty)
            {
                try
                {
                    string host = string.Empty;
                    int current = tctPageMain.SelectedIndex;
                    foreach (WebBrowser web in tctPageMain.TabPages[current].Controls)
                    {
                        if (web != null)
                        {
                            host = web.Document.Url.Host;
                        }
                    }

                    if (LoadCatid(host) == string.Empty)    //本地不存在分类ID时
                    {
                        string src = string.Format("{0}#{1}\n", txtP_Catid.Text.Trim(), host);
                        File.AppendAllText("itemCid.txt", src, Encoding.UTF8);
                        tolState.Text = "保存成功!";
                    }
                    else
                    {
                        tolState.Text = "分类ID已存在无需重复保存。";
                    }
                }
                catch { }
            }
            else
            {
                tolState.Text = "分类或网址为空!";
            }
        }
        //导入新闻
        private void btnToNews_Click(object sender, EventArgs e)
        {
            if (this.btnToNews.Text.Contains("导新闻"))
            {
                this.btnToNews.Text = "完成(&X)";

                //1.禁用相关控件
                groupBox3.Enabled = false;
                groupBox4.Enabled = false;
                groupBox5.Enabled = false;
                groupBox6.Enabled = false;

                //2.绑定控件数据
                using (ToResourceLibrary resource = new ToResourceLibrary())
                {
                    string dbName = string.Empty;
                    while (dbName == string.Empty)
                    {
                        OpenFileDialog op = new OpenFileDialog();
                        op.Reset();
                        op.Filter = "*.MDB|*.MDB";
                        op.FilterIndex = 0;
                        op.RestoreDirectory = true;
                        op.Multiselect = false;
                        op.Title = "选择数据库文件";
                        op.ShowDialog();
                        dbName = op.FileName;
                    }
                    dgvSource.DataSource = resource.GetAllResultNews(dbName);
                }

                //3.初始化控件
                if (dgvSource.Rows.Count != 0)
                {
                    string url = "http://cnc6180.chinaw3.com/Add-HangYe.aspx";
                    string title = "0|导出新闻";
                    AddTablePage(url, title);

                    dgvSource.MouseDoubleClick -= new MouseEventHandler(dgvSource_MouseDoubleClick);
                    dgvSource.MouseDoubleClick += new MouseEventHandler(dgv_ToResourceNewsClick);
                }
            }
            else
            {
                this.btnToNews.Text = "导新闻(&X)";

                //1.启用相关控件
                groupBox3.Enabled = true;
                groupBox4.Enabled = true;
                groupBox5.Enabled = true;
                groupBox6.Enabled = true;
                dgvSource.MouseDoubleClick -= new MouseEventHandler(dgv_ToResourceNewsClick);
                dgvSource.MouseDoubleClick += new MouseEventHandler(dgvSource_MouseDoubleClick);
                this.BindSiteList();
            }
        }
        //导入新闻状态下，双击新闻执行初始化填表
        public void dgv_ToResourceNewsClick(Object send, MouseEventArgs e)
        {
            int current = tctPageMain.SelectedIndex;
            HtmlDocument doc = null;
            bool is_NewsPage = false;
            foreach (WebBrowser web in tctPageMain.TabPages[current].Controls)
            {
                doc = web.Document;
            }
            //检索页面元素
            for (int i = 0; i < doc.All.Count; i++)
            {
                if (doc.All[i].TagName.ToUpper().Equals("INPUT"))   //判断标题元素
                {
                    try
                    {
                        switch (doc.All[i].Name)
                        {
                            case "ctl00$main$TextBox2": //原始标题
                                doc.All[i].InnerText = dgvSource.SelectedRows[0].Cells[0].Value.ToString();
                                is_NewsPage = true;
                                string content = dgvSource.SelectedRows[0].Cells[1].Value.ToString();
                                Clipboard.Clear();
                                Clipboard.SetText(content, TextDataFormat.Html);  //内容复制到剪贴板
                                MouseEvent.SetCursorPos(500, 500);
                                MouseEvent.mouse_event(MouseEvent.MouseEventFlag.LeftDown | MouseEvent.MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
                                break;
                        }
                    }
                    catch { }
                }
            }
            if (is_NewsPage == true)
                tolState.Text = "检测到新闻导出元素，内容在剪切板！";
            else
                tolState.Text = "非新闻导出页面,请重现选择...";
        }
        //检查选项卡,为进一步Post产品做准备
        private void btnP_Post_Click(object sender, EventArgs e)
        {
            CheckControl();

            txtP_Captche.Clear();
            txtP_Answer.Focus();
        }
        #endregion

        #region POST批发发送
        /// <summary>
        /// 批量发送链接
        /// </summary>
        /// <param name="web">WebBrowse对象导航至添加链接页面</param>
        public void PostLink(WebBrowser web)
        {
            Randomlink link = new Randomlink();            
            string referer = web.Url.AbsoluteUri;
            string heads = "Content-Type:application/x-www-form-urlencoded\nReferer:" + referer;
            if (!referer.Contains("action"))
            {
                if (MessageBox.Show("当前页面看起来好像不是“添加链接”的页面,确定继续吗？", "提示",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question)
                    == DialogResult.No)
                {
                    tolState.Text = "用户取消了操作...";
                    return;
                }
            }
            while(true)
            {
                if (MessageBox.Show("提示：请等待提示“添加成功。", "人工干预",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Information)
                        == DialogResult.No)
                {
                    tolState.Text = "用户取消了操作...";
                    return;
                }
                else
                {
                    link = RandLink();
                    link_num++;
                    string post = string.Format("action=add&post[title]={0}&post[style]=&post[linkurl]={1}&post[listorder]=0&submit=提交", link.title, link.url);
                    byte[] byteDate = Encoding.GetEncoding(web.Document.Encoding).GetBytes(post);
                    web.Navigate(referer, "_self", byteDate, heads);
                }
            }
        }
        /// <summary>
        /// 批量添加新闻
        /// </summary>
        /// <param name="meth">代码模式:2(HTML),3(UBB),其他(HTML)</param>
        private void PostMethNews(int meth)
        {
            if (tctPageMain.TabCount != 0)  //判断选项卡总数是否等于0
            {
                int current = tctPageMain.SelectedIndex;
                foreach (WebBrowser web in tctPageMain.TabPages[current].Controls)
                {
                    PostNews(web, meth);
                }
                GC.Collect();
            }
            else
            {
                tolState.Text = "请先打开一个网站!";
            }
        }
        /// <summary>
        /// 批量添加新闻
        /// </summary>
        /// <param name="web">WebBrowse对象</param>
        /// <param name="meth">代码模式:2(HTML),3(UBB),其他默认为HTML</param>
        public void PostNews(WebBrowser web,int meth)
        {           
            string referer = web.Url.AbsoluteUri;
            string heads = "Content-Type:application/x-www-form-urlencoded\nReferer:" + referer;
            if (!referer.Contains("action"))        //判断是否是添加新闻页面
            {
                if (MessageBox.Show("当前页面看起来好像不是“添加新闻”的页面,确定继续吗？", "提示",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question)
                    == DialogResult.No)
                {
                    tolState.Text = "用户取消了操作...";
                    return;
                }
            }
            if (meth != 3) meth = 2;                //设置代码模式
            Business bus = new Business();
            DataTable dt = new DataTable();
            dt = bus.GetItemCatNews("行业新闻");    //获取指定分类新闻
            if (dt.Rows.Count != 0)
            {
                while (true)
                {
                    if (newsCurrent >= dt.Rows.Count) newsCurrent = 0;
                    if (MessageBox.Show("提示：请等待提示“添加成功。", "人工干预",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Information)
                        == DialogResult.No)
                    {
                        tolState.Text = "用户取消了操作...";
                        return;
                    }
                    else
                    {
                        string post = string.Format("action=add&post[title]={0}&post[style]=&post[content]={1}&post[addtime]={2}&post[typeid]=0&submit=确定",
                            dt.Rows[newsCurrent][1].ToString(),
                            dt.Rows[newsCurrent][meth].ToString(),  //根据代码模式,发送制定代码
                            DateTime.Now.ToString());
                        byte[] byteDate = Encoding.GetEncoding(web.Document.Encoding).GetBytes(post);
                        web.Navigate(referer, "_self", byteDate, heads);
                        newsCurrent++;
                    }
                }
            }
            else
            {
                MessageBox.Show("请先添加新闻!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }
        #endregion

        #region 导出账号
        /// <summary>
        /// 设置账号信息
        /// </summary>
        /// <param name="send"></param>
        /// <param name="e"></param>
        public void dgv_MouseClick(Object send, MouseEventArgs e)
        {
            int current = tctPageMain.SelectedIndex;
            HtmlDocument doc = null;
            bool is_UserPage = false;
            foreach (WebBrowser web in tctPageMain.TabPages[current].Controls)
            {
                doc = web.Document;
            }
            for (int i = 0; i < doc.All.Count; i++)
            {
                if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
                {
                    try
                    {
                        switch (doc.All[i].Name)
                        {
                            case "ctl00$main$TextBox1": //网址名称
                                doc.All[i].InnerText = dgvSource.SelectedRows[0].Cells[1].Value.ToString();
                                is_UserPage = true;
                                break;
                            case "ctl00$main$TextBox7": //网址
                                doc.All[i].InnerText = dgvSource.SelectedRows[0].Cells[2].Value.ToString();
                                break;
                            case "ctl00$main$TextBox2": //商铺
                                doc.All[i].InnerText = dgvSource.SelectedRows[0].Cells[5].Value.ToString();
                                break;
                            case "ctl00$main$TextBox3": //账号
                                doc.All[i].InnerText = dgvSource.SelectedRows[0].Cells[3].Value.ToString();
                                break;
                            case "ctl00$main$TextBox4": //密码
                                doc.All[i].InnerText = dgvSource.SelectedRows[0].Cells[4].Value.ToString();
                                break;
                            case "ctl00$main$TextBox6": //备注
                                doc.All[i].InnerText = dgvSource.SelectedRows[0].Cells[6].Value.ToString();
                                break;
                        }
                    }
                    catch { }
                }
            }
            if (is_UserPage == true)
                tolState.Text = "检测到用户导出元素...";
            else
                tolState.Text = "非用户导出页面,请重现选择...";
        }
        #endregion

        #region 公司简介
        /// <summary>
        /// 设置公司基本资料
        /// </summary>
        /// <param name="doc">Document对象</param>
        public void SetCompanyInfo(HtmlDocument doc)
        {
            /*
             * 20110623添加了公司形象图片，更新了：手机号码、传真、主页、公司简介。
             * 20110624增加一个公司注册地、QQ号码元素识别。
             * 20110701在公司简介中增加了备用网站的首页链接，公司主页改为备用网站球磨机的链接。
             */

            #region 公司简介文本
            string companyInfo = "河南红星矿山机器有限公司(<a href='http://www.fuhepo.cn/'>http://www.fuhepo.cn</a>)是一家以生产重型矿山机器为主，集科研、生产、销售为一体的大型股份<a href='http://www.371jq.cn/news/464.html'>颚式破碎机厂家</a>制企业。公司<a href='http://www.redstarjq.com/Chanpin/120.htm'>水泥生产工艺</a>位于郑州国家高新技术产业开发区，占地十万平方米，拥有60000平方米标准化重型工业厂房，各种大、中型金加工、铆焊、装配设备200余<a href='http://www.51hxjq.com/n687.htm'>制砂机</a>台，在线员工960余人，其中具有中、高级职称的管理干部和工程技术人员120余人。公司先后引进了<a href='http://www.hnhxjq.cn/product/106.htm'>雷蒙磨</a>美国、德国、日本、澳<a href='http://www.371hxjq.com/PsSb/Product_398.html'>圆锥破碎机价格</a>大利亚等国家的先进技术和工艺，建立了国际先进的生产线和一流的现代化检测基地，并成立了砂石机器研究室、<a href='http://www.hxpsj.cn/lmm/gqm.html'>磨粉机</a>器研究室、<a href='http://www.zhisha.cn/9.htm'>选矿设备</a>研究室。公司以科学的管理方法，精益求精的制造工艺，勇于创新的制造理念迅速壮大成长为中国矿山机械生产和出口基地。<br />公司视质量为生命，奉用户为上<a href='http://www.hnhxjq.cn/shownews.asp?id=836'>振动筛厂家</a>帝，并一贯遵循对每一道工序负责，对每一台产品负责，对每一位用户负责的质量方针，竭诚为用户服务。现所有产品已通过ISO9001国际质量体系认证，公司生产的“红星牌”机器被评为“中国著名品牌”。可靠稳定的质量使我公司产销量及各项综合经济指标居国内同行业之前列，数万家用户遍布全国，并远销三十多个国家。<br />公司主要产品有破碎设备（<a href='http://www.371jq.cn/1.html'>颚式破碎机</a>、<a href='http://www.371jq.cn/3.html'>反击式破碎机</a>、<a href='http://www.hxjiqi.com/pro/20104.html'>冲击式破碎机</a>、<a href='http://www.hnhxjq.cn/shownews.asp?id=848'>圆锥式破碎机</a>、<a href='http://www.hxpsj.cn/fhspsj/fhspsjgzyl.html'>复合式破碎机</a>、高效<a href='http://www.chuipo.com/xisj.htm'>细碎机</a>、<a href='http://www.zhisha.cn/show_cp.asp?id=79'>锤式破碎机</a>、<a href='http://www.hxjiqi.com/art/201059.html'>辊式破碎机</a>、单段式破碎机、<a href='http://www.51hxjq.com/12.htm'>圆振动筛</a>等成套破碎设备）、<a href='http://www.hnhxjq.cn/zsj/structure.htm'>制砂设备</a>（<a href='http://www.hnhxjq.cn/'>制砂机</a>、颚式破碎机（细破）、<a href='http://www.zhisha.cn/show_cp.asp?id=22'>振动筛</a>、胶带式<a href='http://www.hxpsj.cn/hzy/ssj.html'>输送机</a>、新型高效制砂机、<a href='http://www.rsjq.org/art/19.html'>洗砂机</a>等成套<a href='http://www.51hxjq.com/n663.htm'>制砂生产线</a>设备）、选矿设备（<a href='http://www.hnhxjq.cn/shownews.asp?id=764'>球磨机</a>、<a href='http://www.chuipo.com/fxj.htm'>浮选机</a>、<a href='http://www.371jq.cn/27.html'>分级机</a>、<a href='http://www.posui.com.cn/newslist_118.html'>磁选机</a>、成品筛、<a href='http://www.rsjq.org/art/11.html'>摇床</a>、给矿机、<a href='http://www.chuipo.com/hgj.htm'>烘干机</a>等成套选矿设备）、<a href='http://www.371jq.cn/17.html'>磨粉设备</a>（<a href='http://www.hnhxjq.cn/product/106.htm'>雷蒙磨粉机</a>、<a href='http://www.zhisha.cn/show_jswz.asp?id=2268'>高强磨</a>粉机、<a href='http://www.chuipo.com/gywfm.htm'>高压微粉磨</a>、斗式提升机、颚式破碎机、电磁<a href='http://www.posui.com.cn/glj.htm'>振动给料机</a>等工业磨粉成套设备）四大系列，广泛应用于冶金、矿山<a href='http://www.51hxjq.com/22.htm'>振动给料机</a>、化工、建材、煤炭、耐火材料、陶瓷等行业。球磨机、磁选机 、浮选机、分级机等成套选矿设备，适用于选铁、铜、金、银、铅、钨、钼<a href='http://www.chuipo.com/cpzs/175.htm'>转鼓烘干机</a>、锰等有色金属<a href='http://www.hxpsj.cn/zds/zds.html'>郑州振动筛</a>矿及萤石、石英石等非金属矿行业；颚式破碎机、反击式破碎机、制砂机、振动筛、洗<a href='http://www.redstarjq.com/problem.asp?id=249'>复合肥生产线</a>砂机、输送机等砂石破碎设备适用于机场、铁路<a href='http://www.371jq.cn/'>锤式粉碎机</a>、高速公路、普通公路修筑及国家大型桥梁、海底隧道、陆地隧道、新型高层楼房等建筑。 同时公司可为用户提供项目设计，工艺流程设计，标准和非标准产品设计及生产。<br /> 公司自成立以来，始终坚持“思路决定出路，人品铸就产品”的经营理念，连续多年被评为“文明企业”、“重点骨干企业”、“质量达标企业”、“重合同，守信用”企业，客户赠于“这里信得过”称誉。红星立足中原，覆盖全国，面向世界，以人才为根本，以创新为导向，坚定不移地把矿山机械产业做大做强做久；在此基础上，公司加大市场拓展力度和资本运营力度，积极<a href='http://www.rsjq.org/art/n5.html'>石料生产线厂家</a>主动地寻求新的发展空间，谨慎务实地探索新的战略领域，向着集团化、国际化的目标发展，为实现公司更大的成功奠定基础，创造条件,使红星牌成为中国矿山机械第一品牌。我厂生产的<a href='http://www.371hxjq.com/XkZs/201008/Show_498.html'>水泥生产工艺流程</a>，<a href='http://www.hxpsj.cn/espsj/psjjg.html'>碎石机价格</a>，<a href='http://www.371jq.cn/48.html'>辊式破碎机</a>，<a href='http://www.371hxjq.com/525.html'>高强磨</a>，<a href='http://www.hxpsj.cn/zsj/zssb.html'>制砂设备</a>等设备深受客户好评！<br>郑州红星研制的砂石生产线设备：	均采用创新技术，使制砂破碎生产效率一步步提高。红星坚持创新，不断研发，创新技能，希望为整个狂想制造业输送新鲜的 血液，带来更强的生命力。<br />相关文章：<br />振动筛厂家: http://www.hxpsj.cn/zds/zxzdsgzyl.html<br/>磨粉设备: http://www.hxjiqi.com/art/201056.html<br/>高强磨: http://www.zhisha.cn/p7.htm<br/>水泥机械: http://www.371hxjq.com/XkZs/318.html<br/>煤炭粉碎机: http://www.hnhxjq.cn/shownews.asp?id=775";
            #endregion
            bool Is_CompanyPage = false;
            for (int i = 0; i < doc.All.Count; i++)
            {
                if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
                {
                    #region 查询公司简介相关元素                    
                    try
                    {
                        switch (doc.All[i].Name)
                        {
                            case "post[department]":    //部门
                                doc.All[i].InnerText = "销售部";
                                Is_CompanyPage = true;
                                break;
                            case "post[buy]":           //采购产品
                                doc.All[i].InnerText = "破碎设备|制砂设备|选矿设备|磨粉设备|矿山设备";
                                break;
                            case "post[regcity]":       //公司注册地
                                doc.All[i].InnerText = "河南省郑州市";
                                break;
                            case "post[qq]":            //QQ号码
                                doc.All[i].InnerText = "56451578945";
                                break;
                            case "post[telephone]":     //公司电话
                                doc.All[i].InnerText = "86-0371-67776666";
                                break;
                            case "post[thumb]":         //形象图片
                                doc.All[i].InnerText = "http://www.qiumoji.org.cn/images/00000.gif";
                                break;
                            case "post[mobile]":        //手机号码
                                doc.All[i].InnerText = "15903629989";
                                break;
                            case "post[career]":        //职位
                                doc.All[i].InnerText = "销售部经理";
                                break;
                            case "post[business]":      //经营范围
                                doc.All[i].InnerText = "破碎设备|制砂设备|选矿设备|磨粉设备|矿山设备";
                                break;
                            case "post[regyear]":       //成立年份
                                doc.All[i].InnerText = "1986";
                                break;
                            case "post[capital]":       //注册资金
                                doc.All[i].InnerText = "1090";
                                break;
                            case "post[sell]":          //销售产品
                                doc.All[i].InnerText = "颚式破碎机|反击式破碎机|冲击式破碎机|圆锥式破碎机|复合式破碎机|高效细碎机|锤式破碎机|辊式破碎机|单段式破碎机|圆振动筛|成套破碎设备|制砂设备|制砂机|颚式破碎机（细破）|振动筛|胶带式输送机|新型高效制砂机|洗砂机|成套制砂生产线设备|选矿设备|球磨机|浮选机|分级机|磁选机|成品筛|摇床|给矿机|烘干机|成套选矿设备|磨粉设备|雷蒙磨粉机|高强磨粉机|高压微粉磨|斗式提升机|颚式破碎机|电磁振动给料机|工业磨粉成套设备";
                                break;
                            case "post[address]":       //经营地点
                                doc.All[i].InnerText = "河南省郑州市高新区檀香路8号";
                                break;
                            case "post[postcode]":      //邮政编码
                                doc.All[i].InnerText = "450001";
                                break;
                            case "post[fax]":           //传真
                                doc.All[i].InnerText = "86-0371-67825133";
                                break;
                            case "post[mail]":          //公司Mail
                                doc.All[i].InnerText = "server@taobaolm.org";
                                break;
                            case "post[homepage]":      //主页  此处公司主页为 测试性功能。
                                doc.All[i].InnerText = "http://www.qiumoji.org.cn/";
                                break;
                            case "submit":
                                doc.All[i].Focus();
                                Clipboard.Clear();
                                Clipboard.SetText(companyInfo,TextDataFormat.Html);
                                break;
                        }
                    }
                    catch { }
                    #endregion
                }
            }
            if (Is_CompanyPage)
            {
                tolState.Text = "公司简介已加载到剪贴板中，请执行粘贴操作...";
            }
            else
            {
                tolState.Text = "非公司基本资料登录页面，请转到公司基本资料页面，然后重新检测...";
            }
        }
        #endregion

        #region 初始化登录页面
        /// <summary>
        /// 初始化登录页面用户名和密码
        /// </summary>
        /// <param name="doc">Document对象</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        public void SetLoginUser(HtmlDocument doc, string username, string password)
        {
            bool Is_LoginPage = false;
            for (int i = 0; i < doc.All.Count; i++)
            {
                if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
                {
                    try
                    {
                        switch (doc.All[i].Name)
                        {
                            case "username":
                                doc.All[i].InnerText = username;
                                Is_LoginPage = true;
                                break;
                            case "password":
                                doc.All[i].InnerText = password;
                                break;
                            case "submit":
                                doc.All[i].Focus();
                                break;
                        }
                    }
                    catch { }
                }
            }
            if (Is_LoginPage)
            {
                tolState.Text = "检测到登录元素,已经识别...";
            }
            else
            {
                tolState.Text = "非用户登录页面，请转到用户登录页面，然后重新检测...";
            }
        }
        #endregion

        #region 生成产品
        /// <summary>
        /// 检测添加产品页面
        /// </summary>
        /// <param name="meth">代码模式:2(HTML),3UBB,其他默认HTML</param>
        private void AutoAddProducts(int meth)
        {
            if (tctPageMain.TabCount != 0)  //判断选项卡总数是否等于0
            {
                int current = tctPageMain.SelectedIndex;
                HtmlDocument doc = null;
                foreach (WebBrowser web in tctPageMain.TabPages[current].Controls)
                {
                    doc = web.Document;
                }
                AddProducts(doc, meth);
            }
            else
            {
                tolState.Text = "请先打开一个网站!";
            }
        }

        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="doc">添加产品页面Document对象</param>
        /// <param name="meth">内容模式:2(HTML),3(UBB),其他默认为HTML</param>
        public void AddProducts(HtmlDocument doc, int meth)
        {
            Business bus = new Business();
            DataTable dt = new DataTable();
            dt = bus.GetItemCatNews("供求信息");
            if (dt.Rows.Count != 0)
            {
                //指针标志大于产品总数,重置0
                if (productsCurrent >= dt.Rows.Count) productsCurrent = 0;
                bool Is_ProductsPage = false;
                if (meth != 3) meth = 2;            //设定产品内容代码模式

                #region 检索页面元素
                for (int i = 0; i < doc.All.Count; i++)
                {
                    if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
                    {
                        try
                        {
                            string title = dt.Rows[productsCurrent][1].ToString();  //产品标题
                            if (title.Length > 28) title = title.Substring(0, 28);


                            switch (doc.All[i].Name)
                            {
                                case "post[title]":     //产品标题
                                    doc.All[i].InnerText = title;
                                    Is_ProductsPage = true;
                                    Clipboard.Clear();
                                    Clipboard.SetText(dt.Rows[productsCurrent][meth].ToString());  //内容复制到剪贴板
                                    tolState.Text = "提示:产品内容已经复制到剪贴板。执行Ctrl+V键，将内容复制到内容框! --- ";
                                    tolState.Text += dt.Rows[productsCurrent][1].ToString();
                                    break;
                                case "post[tag]":   //关键字
                                    if (title.Length > 15) doc.All[i].InnerText = title.Substring(0, 15);
                                    else doc.All[i].InnerText = title;
                                    break;
                                case "post[thumb]": //产品图片
                                    doc.All[i].InnerText = SearchPic(title);
                                    break;
                                case "post[catid]": //分类
                                    doc.All[i].InnerText = txtP_Catid.Text.Trim();
                                    break;
                                case "answer":      //激活验证问题
                                    doc.All[i].Focus();
                                    break;
                                case "captcha":      //激活验证码
                                    doc.All[i].Focus();
                                    break;
                            }
                        }
                        catch { }
                    }
                }
                #endregion

                if (Is_ProductsPage)
                    productsCurrent++;  //指针递增1
                else
                    tolState.Text = "注意:非产品添加页面";
            }
            else
            {
                MessageBox.Show("请先添加“产品”！", "提示",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Asterisk);
                tolState.Text = "没有产品可以添加...";
            }
        }

        /// <summary>
        /// 根据标题返回产品图片Url地址
        /// </summary>
        /// <param name="title">关键字</param>
        /// <returns>产品图片Url地址</returns>
        public string SearchPic(string title)
        {
            string url = "http://www.taobaolm.org/pics/球磨机.gif"; //默认球磨机
            try
            {
                string[] item = File.ReadAllLines("urlItem.txt", Encoding.UTF8);
                //for (int i = 0; i < item.Length; i++)
                //{
                //    string[] src = item[i].Split('|');
                //    if (title.Contains(src[0].ToString()))    //如果关键字在产品标题中出现
                //    {
                //        url = src[1].ToString();
                //        break;
                //    }
                //}
                Random rd = new Random();
                string[] src = item[rd.Next(item.Length)].Split('|');
                url = src[1].ToString();
            }
            catch { }
            return url;
        }
        #endregion

        #region 生成注册页面信息
        /// <summary>
        /// 初始化页面信息
        /// </summary>
        /// <param name="doc">Document文档</param>
        private void SetPageInfo(HtmlDocument doc)
        {
            bool Is_RegPage = false;    //是否注册页面
            #region 初始化注册表单
            for (int i = 0; i < doc.All.Count; i++)
            {
                if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
                {
                    try
                    {
                        switch (doc.All[i].Name)
                        {
                            case "post[username]":
                                doc.All[i].InnerText = regInfo.username;
                                doc.All[i].Focus();
                                Is_RegPage = true;
                                break;
                            case "post[passport]":
                                doc.All[i].InnerText = regInfo.passport;
                                doc.All[i].Focus();
                                break;
                            case "post[password]":
                                doc.All[i].InnerText = regInfo.password;
                                doc.All[i].Focus();
                                break;
                            case "post[cpassword]":
                                doc.All[i].InnerText = regInfo.password;
                                doc.All[i].Focus();
                                break;
                            case "post[truename]":
                                doc.All[i].InnerText = regInfo.truename;
                                doc.All[i].Focus();
                                break;
                            case "post[email]":
                                doc.All[i].InnerText = regInfo.email;
                                doc.All[i].Focus();
                                break;
                            case "post[mobile]":
                                doc.All[i].InnerText = regInfo.mobile;
                                doc.All[i].Focus();
                                break;
                            case "post[company]":
                                doc.All[i].InnerText = regInfo.company;
                                doc.All[i].Focus();
                                break;
                            case "post[telephone]":
                                doc.All[i].InnerText = regInfo.telephone;
                                doc.All[i].Focus();
                                break;
                            case "captcha":
                                doc.All[i].Focus();
                                break;
                            case "answer":
                                doc.All[i].Focus();
                                break;
                        }
                    }
                    catch { }
                }
            }
            #endregion            
            if (Is_RegPage)             //判断是否是注册页面
            {
                tolState.Text = "页面元素检索完毕!";
                btnSave.Enabled = true;
            }
            else
            {
                btnSave.Enabled = false;
                tolState.Text = "注意:非注册页面。";
            }
        }
        /// <summary>
        /// 随机初始化页面值
        /// </summary>
        private void RandomRegInfo()
        {
            Random rd = new Random();
            regInfo.username = string.Format("lo{0}ve{1}", rd.Next(9).ToString(), rd.Next(9).ToString());
            regInfo.email = string.Format("seo00{0}@taobaolm.org",rd.Next(9).ToString());
            regInfo.company = RandCompanyName();
            regInfo.passport = regInfo.username;
            regInfo.mobile = "15903629939";
            regInfo.password = "hongxing";
            regInfo.telephone = "86-0371-67776666";
            regInfo.truename = "杨方楼";
        }
        /// <summary>
        /// 随机生成公司名称
        /// </summary>
        /// <returns>随机生成的公司名称</returns>
        private string RandCompanyName()
        {
            string[] company = new string[] {
                "河南红星机器有限责任公司",
                "河南红星机械有限责任公司",
                "河南红星重工有限责任公司",
                "河南红星重工机器有限责任公司",
                "河南红星重工机械有限责任公司",
                "河南红星工程机械有限责任公司",
                "河南红星矿山机器有限责任公司",
                "河南红星矿山机械有限责任公司",
                "河南红星矿山设备有限责任公司",
                "河南红星选矿设备有限责任公司",
                "河南红星选矿机械有限责任公司",
                "河南红星选矿机器有限责任公司",
                "河南红星路桥机械有限责任公司",
                "河南红星路桥机器有限责任公司",
                "河南红星机器有限公司",
                "河南红星机械有限公司",
                "河南红星重工有限公司",
                "河南红星重工机器有限公司",
                "河南红星重工机械有限公司",
                "河南红星工程机械有限公司",
                "河南红星矿山机器有限公司",
                "河南红星矿山机械有限公司",
                "河南红星矿山设备有限公司",
                "河南红星选矿设备有限公司",
                "河南红星选矿机械有限公司",
                "河南红星选矿机器有限公司",
                "河南红星路桥机械有限公司",
                "河南红星路桥机器有限公司",
                "河南红星路桥重工机器有限公司"};
            Random rd = new Random();
            return company[rd.Next(company.Length)].ToString();
        }
        #endregion

        #region 生成友情链接
        /// <summary>
        /// 生成友情链接
        /// </summary>
        /// <returns>友情链接</returns>
        public Randomlink RandLink()
        {

            /*
             * 20110701将友情链接中最后两个,替换成备用网站球磨机和破碎机。
             */
            #region 初始化链接
            links[0].title = "鄂式破碎机";
            links[0].url = "http://www.hxpsj.cn";
            links[1].title = "颚式破碎机";
            links[1].url = "http://www.371jq.cn";
            links[2].title = "反击式破碎机";
            links[2].url = "http://www.hxjiqi.com";
            links[3].title = "冲击式破碎机";
            links[3].url = "http://www.51hxjq.com";
            links[4].title = "锤式破碎机";
            links[4].url = "http://www.hnhxjq.cn";
            links[5].title = "球磨机";
            links[5].url = "http://www.chuipo.com";
            links[6].title = "磨粉机";
            links[6].url = "http://www.zhisha.cn";
            links[7].title = "制砂机";
            links[7].url = "http://www.rsjq.org";
            links[8].title = "烘干机";
            links[8].url = "http://www.posui.com.cn";
            links[9].title = "振动筛";
            links[9].url = "http://www.371hxjq.com";
            links[10].title = "雷蒙磨";
            links[10].url = "http://www.hnhx.org";
            links[11].title = "选矿设备";
            links[11].url = "http://www.fuhepo.cn";
            links[12].title = "圆锥破碎机";
            links[12].url = "http://www.chuipo.cn";
            links[13].title = "矿山设备";
            links[13].url = "http://www.hxjiqi.com/product.html";
            links[14].title = "回转窑";
            links[14].url = "http://www.371jq.cn/24.html";
            links[15].title = "碎石机";
            links[15].url = "http://www.371jq.cn/8.html";
            links[16].title = "雷蒙磨粉机";
            links[16].url = "http://www.371jq.cn/16.html";
            links[17].title = "复合式破碎机";
            links[17].url = "http://www.371jq.cn/33.html";
            links[18].title = "破碎机专题网";
            links[18].url = "http://www.fuhepo.cn/";
            links[19].title = "球磨机专题网";
            links[19].url = "http://www.qiumoji.org.cn/";
            #endregion

            if (link_num < links.Length)
            {
                return links[link_num];
            }
            else
            {
                link_num = 0;
                return links[link_num];
            }
        }
        #endregion        

        private void Btn_DaoHang_Click(object sender, EventArgs e)
        {
            AddCurrent_Tab(txtBox_DaohangUrl.Text.Trim());
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            try
            {
                if (tab_Current.TabCount != 0)
                {
                    int current = tab_Current.SelectedIndex;
                    foreach (WebBrowser web in tab_Current.TabPages[current].Controls)
                    {
                        web.Document.Body.Style = "zoom:" + trackBar1.Value.ToString() + "%";
                    }
                }
            }
            catch { }
        }

        /// <summary>
        /// 添加选项卡
        /// </summary>
        /// <param name="url">网址</param>
        public void AddCurrent_Tab(string url)
        {
            try
            {
                Uri uri = new Uri(url);
                WebBrowser web = new WebBrowser();
                web.Url = uri;
                web.Dock = DockStyle.Fill;
                web.ScriptErrorsSuppressed = true;
                int count = tab_Current.TabPages.Count;
                Random r = new Random();
                tab_Current.TabPages.Add(r.Next(10).ToString());
                tab_Current.TabPages[count].Controls.Add(web);
                tab_Current.SelectedIndex = count;
            }
            catch
            { }

            if (tab_Current.TabPages.Count == 0) splitContainer1.Panel2Collapsed = true;
            else splitContainer1.Panel2Collapsed = false;
        }

        private void tab_Current_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int current = tab_Current.SelectedIndex;
            foreach (WebBrowser web in tab_Current.TabPages[current].Controls)
            {
                web.Dispose();
            }
            tab_Current.TabPages[current].Dispose();

            if (tab_Current.TabPages.Count == 0) splitContainer1.Panel2Collapsed = true;
            else splitContainer1.Panel2Collapsed = false;
        }        

        //检查当前选项卡
        private void CheckControl()
        {
            if (tctPageMain.TabCount != 0)  //判断选项卡总数是否等于0
            {
                int current = tctPageMain.SelectedIndex;
                foreach (WebBrowser web in tctPageMain.TabPages[current].Controls)
                {
                    PostProducts(web, CreatePostDataProducts());    //发送产品Post数据
                }
                GC.Collect();
            }
            else
            {
                tolState.Text = "请先打开一个网站!";
            }
        }

        /// <summary>
        /// 生成添加产品Post数据
        /// </summary>
        /// <returns>产品Post数据</returns>
        private string CreatePostDataProducts()
        {
            string src = rtxPostData.Text.Trim();
            src = src.Replace("#CATID#", txtP_Catid.Text.Trim());  //替换分类ID
            src = src.Replace("#ANSER#", txtP_Answer.Text.Trim());  //替换问题
            src = src.Replace("#CAPTCHE#", txtP_Captche.Text.Trim());  //替换验证码 
            

            Business bus = new Business();
            DataTable dt = new DataTable();
            dt = bus.GetItemCatNews("行业新闻");
            if (dt.Rows.Count != 0)
            {
                if (newsCurrent >= dt.Rows.Count) newsCurrent = 0;  //指针标志大于产品总数,重置0
                string title = dt.Rows[newsCurrent][1].ToString();      //产品标题
                string content = dt.Rows[newsCurrent][3].ToString();    //UBB内容

                //替换标题
                if (title.Length > 28) title = title.Substring(0, 28);
                src = src.Replace("#TITLE#", title);

                //替换图片
                src = src.Replace("#IMAGE#", SearchPic(title));

                //替换标签
                if (title.Length > 15) title = title.Substring(0, 15);
                src = src.Replace("#TAG#", title);

                //替换内容
                src = src.Replace("#CONTENT#", content);

                newsCurrent++;  //指针递增1
            }
            else
            {
                MessageBox.Show("请先添加“产品”！", "提示",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Asterisk);
                tolState.Text = "没有产品可以添加...";
            }

            return src;
        }

        /// <summary>
        /// 发送Post产品数据
        /// </summary>
        /// <param name="web">当前Web页面对象</param>
        /// <param name="postData">Post数据</param>
        private void PostProducts(WebBrowser web, string postData)
        {
            string referer = web.Url.AbsoluteUri;
            string heads = "Content-Type:application/x-www-form-urlencoded\nReferer:" + referer;
            byte[] byteDate = Encoding.GetEncoding(web.Document.Encoding).GetBytes(postData);
            web.Navigate(referer, "_self", byteDate, heads);
        }

        /// <summary>
        /// 根据网址加载产品常用分类ID编号
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private string LoadCatid(string url)
        {
            string[] itemCid = File.ReadAllLines("itemCid.txt", Encoding.UTF8);
            for (int i = 0; i < itemCid.Length; i++)
            {
                string[] line = itemCid[i].ToString().Split('#'); //line[0]:ID line[1]:Url
                if (line[1].ToString() == url)  //判断是否存在分类ＩＤ
                {
                   return line[0].ToString();  //返回该分类ID
                }
            }
            return "";  //不存在情况下，返回空
        }

        //智能模式下Post发送Html文章
        private void btnAutoHtmlPostNews_Click(object sender, EventArgs e)
        {
            PostMethNews(2);
        }
    }
}